<template>
  <div class="divBox">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <el-input
          v-model="merData.number"
          enter-button="查找位置"
          style="width: 20%;"
          type="number"
          placeholder="请输入核销码的数量"

        >

        </el-input>
        <el-button size="small" type="primary" @click="onAdd">新建核销码</el-button>

      </div>

      <el-table
        v-loading="loading"
        :data="tableData.data"
        style="width: 100%"
        size="small"
      >


        <el-table-column
          prop="id"
          label="ID"
          min-width="80"
        />
        <el-table-column
          prop="code"
          label="核销码"
          min-width="150"
        />
        <el-table-column
          label="使用状态"
          min-width="100"
        >
          <template slot-scope="scope">
            <span v-if="scope.row.status==0">未使用</span>
            <span v-else>已使用</span>
          </template>
        </el-table-column>
        <el-table-column
          prop="order_id"
          label="订单id"
          min-width="100"
        />

        <el-table-column
          prop="create_time"
          label="创建时间"
          min-width="150"
        />
        <el-table-column label="操作" min-width="100" fixed="right">
          <template slot-scope="scope">
            <el-button type="text" size="small" @click="onEdit(scope.row.group_data_id)">编辑</el-button>
            <el-button type="text" size="small" @click="handleDelete(scope.row.group_data_id, scope.$index)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="block">
        <el-pagination
          :page-sizes="[20, 40, 60, 80]"
          :page-size="tableData.limit"
          :current-page="tableData.page"
          layout="total, sizes, prev, pager, next, jumper"
          :total="tableData.total"
          @size-change="handleSizeChange"
          @current-change="pageChange"
        />
      </div>
    </el-card>
  </div>
</template>

<script>

import {
  createGroupData,
  updateGroupData,
  groupDataLst,
  deleteGroupData,
  groupDetail,
  statusGroupData
} from '@/api/system'
import {
  getVerificationList,
  createVerification
} from '@/api/marketing'
import { roterPre } from '@/settings'
import { mapGetters } from 'vuex'
export default {
  name: 'VerificationCreat',
  data() {
    return {
      merData:{
        number:null
      },
      roterPre: roterPre,
      tableData: {
        page: 1,
        limit: 20,
        data: [],
        total: 0
      },
      groupId: null,
      loading: false,
      groupDetail: null,
      titles: ''
    }
  },
  computed: {
    columns() {
      if (!this.groupDetail) return []
      const colums = [
        {
          title: 'ID',
          key: 'group_data_id',
          minWidth: 60
        }]
      if(this.groupDetail.fields)
        this.groupDetail.fields.forEach((val) => {
          colums.push({
            title: val
              .name,
            key:
            val.field,
            minWidth:
              80
          })
        })
      colums.push(
        {
          title: '添加时间',
          key: 'create_time',
          minWidth: 200
        },
        // {
        //   title: '状态',
        //   key: 'status',
        //   minWidth: 80
        // },
        // {
        //   title: '操作',
        //   slot: 'action',
        //   minWidth: 120,
        //   fixed: 'right'
        // }
      )

      return colums
    },
  },
  watch: {
    '$route.params.id': function(n) {
      this.groupId = n
    },
    groupId(n) {
      // this.getGroupDetail(n)
    }
  },
  mounted() {
    this.groupId = this.$route.params.id
  },
  created() {
    // this.tempRoute = Object.assign({}, this.$route)
  },
  methods: {
    getGroupDetail(id) {
      groupDetail(id).then(res => {
        this.groupDetail = res.data
        this.tableData.page = 1
        this.getList()
      }).catch(res => {
        this.$message.error(res.message)
      })
    },
    createVerification(){
      createVerification(this.merData).then(res => {
        // this.tableData.data = res.data.list
        // this.tableData.total = res.data.count
        // this.Loading = false
        this.getVerificationList()
      }).catch(res => {
        this.$message.error(res.message)
      })
    },
    getVerificationList() {
      this.Loading = true
      getVerificationList(this.data).then(res => {
        this.issueData.data = res.data.list
        this.issueData.total = res.data.count
        this.Loading = false
      }).catch(res => {
        this.Loading = false
        this.$message.error(res.message)
      })
    },
    // 列表
    getList() {
      this.loading = false
      groupDataLst(this.$route.params.id, this.tableData.page, this.tableData.limit).then(res => {
        this.tableData.data = res.data.list
        this.tableData.total = res.data.count
        this.loading = false
      }).catch(({ message }) => {
        this.loading = false
        this.$message.error(message)
      })
    },
    pageChange(page) {
      this.tableData.page = page
      this.getList()
    },
    handleSizeChange(val) {
      this.tableData.limit = val
      this.getList()
    },
    // 添加
    onAdd() {
      // this.$modalForm(createGroupData(this.$route.params.id)).then(() => this.getList())
      if(!this.merData.number||this.merData.number=='0'){
        return this.$message.error('请输入数量')
      }
      this.createVerification()
    },
    // 编辑
    onEdit(id) {
      this.$modalForm(updateGroupData(this.$route.params.id, id)).then(() => this.getList())
    },
    onchangeIsShow(row) {
      statusGroupData(row.group_data_id, { status: row.status }).then(({ message }) => {
        this.$message.success(message)
        this.getList()
      }).catch(({ message }) => {
        this.$message.error(message)
      })
    },
    // 删除
    handleDelete(id, idx) {
      this.$modalSure('确定删除该专题').then(() => {
        deleteGroupData(id).then(({ message }) => {
          this.$message.success(message)
          this.tableData.data.splice(idx, 1)
        }).catch(({ message }) => {
          this.$message.error(message)
        })
      })
    },
  }
}
</script>

<style scoped lang="scss">

</style>
